<?php

define('IN_DOUCO', true);

require (dirname(__FILE__) . '/include/init.php');

// rec操作项的初始化
$rec = (isset($_REQUEST['rec']) && $check->is_rec($_REQUEST['rec'])) ? $_REQUEST['rec'] : 'default';

// 赋值给模板
$smarty->assign('rec', $rec);
$smarty->assign('cur', 'index_' . $rec);

/**
 * +----------------------------------------------------------
 * 系统信息
 * +----------------------------------------------------------
 */
if ($rec == 'default') {
    $sys_info['os'] = PHP_OS;
    $sys_info['ip'] = $_SERVER['SERVER_ADDR'];
    $sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE'];
    $sys_info['php_ver'] = PHP_VERSION;
    $sys_info['mysql_ver'] = $dou->version();
    $sys_info['gd'] = extension_loaded("gd") ? $_LANG['yes'] : $_LANG['no'];
    $sys_info['charset'] = strtoupper(DOU_CHARSET);
    $sys_info['build_date'] = date("Y-m-d", $_CFG['build_date']);
    $update_date = unserialize($_CFG['update_date']);
    $sys_info['update'] = $update_date['system']['update'];
    $sys_info['patch'] = $update_date['system']['patch'];
    $sys_info['logo'] = ROOT_URL . 'theme/' . $_CFG['site_theme'] . '/images/' . $_CFG['site_logo'];
    $sys_info['max_filesize'] = ini_get('upload_max_filesize');
    $sys_info['num_page'] = $dou->num_rows($dou->query("SELECT * FROM " . $dou->table('page')));
    $sys_info['num_product'] = $dou->num_rows($dou->query("SELECT * FROM " . $dou->table('product')));
    $sys_info['num_article'] = $dou->num_rows($dou->query("SELECT * FROM " . $dou->table('article')));
    // 提示应该被删除的目录未被删除
    if ($dou->dir_status(ROOT_PATH . 'install') != 'no_exist') $warning[] = $_LANG['warning_install_exists'];
    if ($dou->dir_status(ROOT_PATH . 'upgrade') != 'no_exist') $warning[] = $_LANG['warning_upgrade_exists'];
    if ($extend == 'on') $warning[] = $_LANG['warning_extend_exists'];
    // 写入目录监测信息
    $sys_info['folder_exists'] = $warning;

    //新增数据统计
    $today = array('start'=>strtotime(date('Ymd 00:00:00')),'end'=>strtotime(date('Ymd 23:59:59')));
    $sys_info['today_count'] = $dou->get_one("SELECT count(*) count FROM ".$dou->table('guestbook')."WHERE add_time>'{$today[start]}' AND add_time<'{$today[end]}'");

    // 赋值给模板
    $smarty->assign('dou_api', $dou->dou_api());
    $smarty->assign('cur', 'index');
    $smarty->assign('page_list', $dou->get_page_nolevel());
    $smarty->assign('sys_info', $sys_info);
    $smarty->assign("log_list", $dou->get_admin_log($_SESSION[DOU_ID]['user_id'], 4));
    $smarty->assign('localsite', $dou->dou_localsite());
    
    $smarty->display('index.htm');
} 

elseif($rec == 'hide_leftnav'){
    $hide_leftnav = i('hide',0);
    $_SESSION['hide_leftnav'] = $hide_leftnav;
    $dou->dou_header($_SERVER['HTTP_REFERER']);
}

/**
 * +----------------------------------------------------------
 * 清除缓存及已编译模板
 * +----------------------------------------------------------
 */
elseif ($rec == 'clear_cache') {
    $dou->dou_clear_cache(ROOT_PATH . 'cache');
    $dou->dou_msg($_LANG['clear_cache_success']);
}


//ajax请求
elseif($rec == 'ajax'){
    $op = empty($_REQUEST['op']) ? 'index' : $_REQUEST['op'];
    $ac = empty($_REQUEST['ac']) ? 'index' : $_REQUEST['ac'];
    if($op == 'live' && $ac == 'channel_info'){
        $_rs = $live->channel_info($_REQUEST['pullId']);
        to_ajax(200,'',$_rs);
    }
    if($op == 'live' && $ac == 'stop_live'){
        $live_time_id = $_REQUEST['live_time_id'];
        $state = $_REQUEST['state'];
        $live_time = $dou->get_row("SELECT lt.*,la.pullid FROM ".$dou->table('live_time')." lt ".
            "LEFT JOIN ".$dou->table('live_anchor')." la ON lt.anchor_id=la.id WHERE lt.id='{$live_time_id}'");
        $dou->query($dou->update_sql('live_anchor', array('state'=>$state), "pullid='{$live_time['pullid']}'"));
        //todo 推送禁止用户消息
        $room_id  = $dou->get_one("SELECT roomnumber FROM ".$dou->table('chatroom')." WHERE id='{$live_time['room_id']}'");
        $push_arr = array('room_id'=>$room_id, 'anchor_id'=>$live_time['anchor_id'], 'state'=>$state);
        push_hiddenLive($push_arr);
        to_ajax(200,'', array('state'=>$_REQUEST['state']));
    }
    exit;
}

?>